package cn.com.se.sharepictrue.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.com.se.sharepictrue.R;
import cn.com.se.sharepictrue.adapter.ConcernAdapter;
import cn.com.se.sharepictrue.bean.BaseUser;
import cn.com.se.sharepictrue.bean.HomeShareBean;
import cn.com.se.sharepictrue.business.CallBack;
import cn.com.se.sharepictrue.business.NetworkResponse;
import cn.com.se.sharepictrue.business.ShareApi;
import cn.com.se.sharepictrue.quickadapter.BaseAdapterHelper;
import cn.com.se.sharepictrue.quickadapter.QuickAdapter;
import cn.com.se.sharepictrue.utils.ToastUtils;
import cn.com.se.sharepictrue.widget.LoadMoreListener;

/**
 * 我的关注、我的粉丝界面
 * Created by KIDNG on 2015/11/4.
 */
public class ConcernActivity extends ActionBarActivity implements SwipeRefreshLayout.OnRefreshListener {
    public static final String CONCERN_WHAT_TITLE = "concern_title_key";
    public static final String CONCERN_BUNDLE_KEY = "concern_bundle_key";
    @Bind(R.id.tb_concern_title)
    Toolbar mTbConcernTitle;
    @Bind(R.id.lv_concern_list)
    ListView mLvConcernList;
    @Bind(R.id.srl_concern_refresh)
    SwipeRefreshLayout mSrlConcernRefresh;
    private List<BaseUser> mBeans;
    private ConcernAdapter mAdapter;
    private int CONCERN = 1;
    private int FANS = 2;
    private int what;
    private int nowPage =1;
    private LoadMoreListener mLoadMoreListener;
    private int mLoadInLastRow = 4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_concern);
        ButterKnife.bind(this);

        setToolBar();
        initRefreshView();
        initList();
    }

    private void setToolBar() {
        Intent intent = getIntent();
        String titleText = getResources().getString(R.string.concern_default_title);
        if (null != intent && !intent.getStringExtra(CONCERN_WHAT_TITLE).isEmpty()) {
            titleText = intent.getStringExtra(CONCERN_WHAT_TITLE);
            if (titleText.equals(getResources().getString(R.string.drawer_mine_following))) {
                what = CONCERN;
            } else if (titleText.equals(getResources().getString(R.string.drawer_mine_follower))) {
                what = FANS;
            }
        }
        mTbConcernTitle.setTitle(titleText);
        mTbConcernTitle.setTitleTextColor(Color.WHITE);
        setSupportActionBar(mTbConcernTitle);
    }

    private void initList() {
        if (mBeans == null) return;
        mAdapter = new ConcernAdapter(this, R.layout.item_person_list, mBeans);
        mLvConcernList.setAdapter(mAdapter);
        mAdapter.setConcernListener(new ConcernAdapter.toConcernListener() {
            @Override
            public void toConcern(Button v, BaseUser item) {
                concernPerson(v, item);
            }
        });
        initLoadMoreListener();
        mLvConcernList.post(new Runnable() {
            @Override
            public void run() {
                if (mLvConcernList != null) {
                    mLvConcernList.setOnScrollListener(mLoadMoreListener);
                }
            }
        });
    }

    /**
     * 点击关注按键
     * @param v
     * @param item
     */
    private void concernPerson(Button v, final BaseUser item) {
        String concernId = item.getConcernUserId();
        if (concernId == null) ToastUtils.show(this, "请登录");
        else
            ShareApi.concern(this,
                    what==CONCERN?item.getConcernUserId():item.getUserId(),
                    !item.isConcern(),
                    new CallBack<NetworkResponse<String>>(this) {
                @Override
                public void onFail() {
                    ToastUtils.show(ConcernActivity.this, "操作失败");
                }

                @Override
                public void onResponse(NetworkResponse<String> response) {
                    if (!response.isStatus())
                        ToastUtils.show(ConcernActivity.this, response.getError());
                    else {
                        if (item.isConcern())
                            item.setConcernHim(BaseUser.NO_CONCERN);
                        else
                            item.setConcernHim(BaseUser.IS_CONCERN);
                        mAdapter.notifyDataSetChanged();
                    }
                }
            });

    }

    private void initRefreshView() {
        mSrlConcernRefresh.setOnRefreshListener(this);
        mSrlConcernRefresh.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
        mSrlConcernRefresh.post(new Runnable() {
            @Override
            public void run() {
                mSrlConcernRefresh.setRefreshing(true);
                initData();
            }
        });
    }

    private void initData() {
        nowPage  = 1;
        if (what == CONCERN) {
            ShareApi.getMyConcernList(this,nowPage, new CallBack<NetworkResponse<List<BaseUser>>>() {
                @Override
                public void onFail() {
                    mSrlConcernRefresh.setRefreshing(false);
                    ToastUtils.show(ConcernActivity.this, "加载失败");
                }

                @Override
                public void onResponse(NetworkResponse<List<BaseUser>> response) {
                    mSrlConcernRefresh.setRefreshing(false);
                    if (!response.isStatus()) {
                        ToastUtils.show(ConcernActivity.this, response.getError());
                    } else {
                        mBeans = response.getData();
                        initList();
                    }
                }
            });
        } else if (what == FANS) {
            ShareApi.getMyFansList(this,nowPage, new CallBack<NetworkResponse<List<BaseUser>>>() {
                @Override
                public void onFail() {
                    mSrlConcernRefresh.setRefreshing(false);
                    ToastUtils.show(ConcernActivity.this, "加载失败");
                }

                @Override
                public void onResponse(NetworkResponse<List<BaseUser>> response) {
                    mSrlConcernRefresh.setRefreshing(false);
                    if (!response.isStatus()) {
                        ToastUtils.show(ConcernActivity.this, response.getError());
                    } else {
                        mBeans = response.getData();
                        initList();
                    }
                }
            });
        }
    }
    private void initLoadMoreListener() {
        mLoadMoreListener = new LoadMoreListener(mLoadInLastRow) {
            @Override
            public void load() {
                nowPage++;
                if (what == CONCERN) {
                    ShareApi.getMyConcernList(ConcernActivity.this,nowPage, new CallBack<NetworkResponse<List<BaseUser>>>() {
                        @Override
                        public void onFail() {
                            ToastUtils.show(ConcernActivity.this, "加载失败");
                        }

                        @Override
                        public void onResponse(NetworkResponse<List<BaseUser>> response) {
                            if (!response.isStatus()) {//加载失败
                                ToastUtils.show(ConcernActivity.this, response.getError());
                            } else {
                                addToList(response.getData());
                                mLoadMoreListener.setLoading(false);
                            }
                            if (response.getHasMore() != null&&response.getHasMore().equals(NetworkResponse.NO_MORE)) {
                                mLvConcernList.setOnScrollListener(null);
                            }
                        }
                    });
                } else if (what == FANS) {
                    ShareApi.getMyFansList(ConcernActivity.this,nowPage, new CallBack<NetworkResponse<List<BaseUser>>>() {
                        @Override
                        public void onFail() {
                            ToastUtils.show(ConcernActivity.this, "加载失败");
                        }

                        @Override
                        public void onResponse(NetworkResponse<List<BaseUser>> response) {
                            if (!response.isStatus()) {//加载失败
                                ToastUtils.show(ConcernActivity.this, response.getError());
                            } else {
                                addToList(response.getData());
                                mLoadMoreListener.setLoading(false);
                            }
                            if (response.getHasMore() != null&&response.getHasMore().equals(NetworkResponse.NO_MORE)) {
                                mLvConcernList.setOnScrollListener(null);
                            }
                        }
                    });
                }
            }
        };
    }

    private void addToList(List<BaseUser> data) {
        if (data != null) {
            mAdapter.addAll(data);
            mAdapter.notifyDataSetChanged();
        }
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onRefresh() {
        initData();
    }
}
